<?php
/**
 * Project:     Rebel PHP Framework
 * File:        Rebel_Std_Error_Handler.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.rebelphpframework.org
 * @copyright 2007 Emanuele Colonnelli
 * @author Emanuele Colonnelli <emanuele.colonnelli at gmail dot com>
 * @package Rebel_Std_Error
 * @version 1.0
 */

if(define(__FILE__,true)) {
	
	class Rebel_Std_Error_Handler {
		
		const 	MSG_ADAPTER_NOT_FOUND = 'Could not find adapter labeled %s',
				ADAPTER_MYSQL = 'MySql',
				ADAPTER_SESSION = 'Session',
				ADAPTER_FILE = 'File';
		
		private static 	$basket = array(),
						$instance;
	
		private function __construct(){

			self::$instance = $this;

		}//:-
		
		public static function getInstance() {
			
			return self::$instance ? self::$instance : new self;
			
		}//:-
		
		public function register($label,$adapter = self::ADAPTER_SESSION) {
			
			self::$basket[$label] = $this->errorHandlerFactory($adapter);
			
		}//:-
		
		private function errorHandlerFactory($adapter) {
			
			$adapterClass = sprintf('Rebel_Std_Error_Adapter_%s',$adapter);
			if(!class_exists($adapterClass)) {
				throw new Exception(sprintf(self::MSG_ADAPTER_NOT_FOUND,$adapter));
			}
			
			$r = new ReflectionClass($adapterClass);
			
			return $r->newInstance();
		}//:-
		
		/**
		 * Returns an error handler adapter, implementing Rebel_Std_Error_I_Adapter interface
		 *
		 * @param $label
		 * @return Rebel_Std_Error_I_Adapter
		 */
		public static function get($label) {
			
			if(!isset(self::$basket[$label])) {
				throw new Exception(sprintf(self::MSG_ADAPTER_NOT_FOUND,$label));
			}
			
			return self::$basket[$label];
			
		}//:-
		
		
	}///:-
	
}


?>